﻿package com.wandian.view.layer
{
	import com.greensock.TweenLite;
	import com.greensock.easing.Quint;
	import com.wandian.data.GameData;
	import com.wandian.util.CLZUtils;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;

	/**
	 * 画布
	 * @author long
	 */
	public class Canvas extends Sprite
	{
		private var _arrMask:Array;
		public static const SHOW_MOVE_1:int=1;
		public static const SHOW_MOVE_2:int=2;
		public static const HIDE_MOVE:int=0;

		public function Canvas()
		{
			this._arrMask=[];
			this.mouseEnabled=false;
			return;
		} 

		public function addChildTo(childDisplay:DisplayObject):DisplayObject
		{
			this.showMask();
            CLZUtils.cacheAsBitmap(childDisplay);
			return this.addChild(childDisplay);
		} 

		public function removeChildTo(childDisplay:DisplayObject, showType:int=0):void
		{
			if (childDisplay != null)
			{
				if (super.contains(childDisplay))
				{
					switch (showType)
					{
						case HIDE_MOVE:
						{
							this.removeMask();
							this.removeChild(childDisplay);
							childDisplay=null;
							break;
						}
						case SHOW_MOVE_1:
						{
							childDisplay.cacheAsBitmap=true;
							TweenLite.to(childDisplay, 0.5, {alpha: 0, onComplete: this.removeMc, onCompleteParams: [childDisplay]});
							break;
						}
						case SHOW_MOVE_2:
						{
							break;
						}
						default:
						{
							break;
						}
					}
				}
			}
			
			return;
		} 

		private function removeMc(childMC:DisplayObject):void
		{
			this.removeMask();
			this.removeChild(childMC);
			childMC=null;
			return;
		} 

		/**
		 * change pixel.sy=10
		 * @param childDisplay
		 * @param sx
		 * @param sy
		 * @param showType
		 */
		public function centerXY(childDisplay:DisplayObject, sx:int=0, sy:int=0, showType:int=2):void
		{
			switch(showType)
			{
				case HIDE_MOVE:
				{
					childDisplay.x = (GameData.config["stageW"] - childDisplay.width) * 0.5 + sx;
					childDisplay.y = (GameData.config["stageH"] - childDisplay.height) * 0.5 + sy;
					break;
				}
				case SHOW_MOVE_1:
				{
					childDisplay.x = (GameData.config["stageW"] - childDisplay.width) * 0.5 + sx;
					childDisplay.y = -childDisplay.height;
					TweenLite.to(childDisplay, 0.7, {x:(GameData.config["stageW"] - childDisplay.width) * 0.5 + sx, y:(GameData.config["stageH"] - childDisplay.height) * 0.5 + sy, ease:Quint.easeOut});
					break;
				}
				case SHOW_MOVE_2:
				{
					childDisplay.x = (GameData.config["stageW"] - childDisplay.width) * 0.5 + sx;
					childDisplay.y = (GameData.config["stageH"] - childDisplay.height) * 0.5 + sy;
					childDisplay.alpha = 0;
					TweenLite.to(childDisplay, 0.3, {alpha:1});
					break;
				}
				default:
				{
					break;
				}
			}
			return;
		} 

		override public function addChild(childDisplay:DisplayObject):DisplayObject
		{
           CLZUtils.cacheAsBitmap(childDisplay);
			return super.addChild(childDisplay);
		} 

		override public function removeChild(childDisplay:DisplayObject):DisplayObject
		{
			if (childDisplay != null)
			{
				if (super.contains(childDisplay))
				{
					return super.removeChild(childDisplay);
				}
			}			
			return null;
		} 

		override public function contains(childDisplay:DisplayObject):Boolean
		{
			return super.contains(childDisplay);
		} 

		public function removeAll():void
		{
			var child:*=undefined;
			while (this.numChildren)
			{

				child=this.removeChildAt(0);
				child=null;
			}
			return;
		} 

		override public function get mouseEnabled():Boolean
		{
			return super.mouseEnabled;
		} 

		override public function set mouseEnabled(childDisplay:Boolean):void
		{
			super.mouseEnabled=childDisplay;
			return;
		} 

		private function showMask():void
		{
			var canvasMask:*=new Sprite();
			canvasMask.graphics.beginFill(0);
			canvasMask.graphics.lineStyle(1, 0);
			canvasMask.graphics.drawRect(0, 0,  GameData.config["stageW"], GameData.config["stageH"]);
			canvasMask.graphics.endFill();
			this._arrMask.push(canvasMask);
			canvasMask.alpha=0;
			this.addChild(canvasMask);
			return;
		} 

		private function removeMask():void
		{
			var maskMc:*=this._arrMask.pop();
			if (this.contains(maskMc))
			{
				this.removeChild(maskMc);
				maskMc=null;
			}
			return;
		} 

	}
}
